package plugins;

import com.alibaba.fastjson.JSON;
import play.Logger;
import play.templates.JavaExtensions;
import utils.NumberUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangzc
 * @since 2017/7/22
 */
public class XJavaExtensions extends JavaExtensions {

    static final long ONE_DAY_MS = 24 * 60 * 60 * 1000;

    /**
     * 获取今天 00:00:00
     * @return
     */
    static Date getToday(){
        Date date = new Date(System.currentTimeMillis());
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return date;
    }

    /**
     * 获取昨天 00:00:00
     * @return
     */
    static Date getYesterday(){
        Date date = new Date(getToday().getTime() - ONE_DAY_MS);
        return date;
    }
    /**
     * 获取前天 00:00:00
     * @return
     */
    static Date getTheDayBeforeYesterday(){
        Date date = new Date(getYesterday().getTime() - ONE_DAY_MS);
        return date;
    }

    /**
     * 将时间格式化成人类友好的形式
     * @param date
     * @return
     */
    public static String niceTime(Date date) {

        Date nowDate = new Date();

        Date today = getToday();
        Date yesterday = getYesterday();
        Date theDayBeforeYesterday = getTheDayBeforeYesterday();

        long now = nowDate.getTime();
        long time = date.getTime();
        long distance = (now - time) / 1000;
        if (distance < 10) { // 10s内
            return "刚刚";
        } else if (distance < 60) { // 1分钟内
            return distance + "秒前";
        } else if (distance < 60 * 60) { // 1小时内
            return (distance / 60) + "分钟前";
        } else if ( date.getTime() > today.getTime()) { //今天
            DateFormat dateFormatWithoutYear = new SimpleDateFormat(
                    "今天 HH:mm");
            return dateFormatWithoutYear.format(date);
        }else if (date.getTime() > yesterday.getTime()) { //昨天
            DateFormat dateFormatWithoutYear = new SimpleDateFormat(
                    "昨天 HH:mm");
            return dateFormatWithoutYear.format(date);
        }else if (date.getTime() > theDayBeforeYesterday.getTime()) { //前天
            DateFormat dateFormatWithoutYear = new SimpleDateFormat(
                    "前天 HH:mm");
            return dateFormatWithoutYear.format(date);
        }else { // 早于前天
            if(date.getYear() != nowDate.getYear()){ // 今年以前
                DateFormat dateFormat = new SimpleDateFormat(
                        "yyyy年MM月dd日");
                return dateFormat.format(date);
            }else{ // 今年
                DateFormat dateFormatWithoutYear = new SimpleDateFormat(
                        "MM月dd日");
                return dateFormatWithoutYear.format(date);
            }
        }
    }

    /**
     * yyyy-MM-dd HH:mm
     * @param date
     * @return
     */
    public static String fullDateTime(Date date) {
        if(date == null){
            return "未知";
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return dateFormat.format(date);
    }

    /**
     * yyyy年MM月dd日
     * @param date
     * @return
     */
    public static String simpleDateTime(Date date) {
        if(date == null){
            return "未知";
        }
        Date nowDate = new Date();
        if(nowDate.getYear() == date.getYear()){
            DateFormat dateFormat = new SimpleDateFormat("MM月dd日");
            return dateFormat.format(date);
        }else{
            DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            return dateFormat.format(date);
        }
    }

    /**
     * yyyy-MM-dd
     * @param date
     * @return
     */
    public static String fullDate(Date date) {
        if(date == null){
            return "未知";
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static String toJSON(Object object) {
        if(object == null){
            return "{}";
        }
        return JSON.toJSONString(object);
    }

    /**
     * 格式化金额分为人民币元
     * @param data
     * @return
     */
    public static String asRmb(Long data){
        if(data == null){
            return "0";
        }
        return NumberUtil.formatAsRMB(data);
    }


    public static String asURI(String val){
        try {
            return URLEncoder.encode(val, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            Logger.error(e, "Error in XJavaExtensions.asURI:" + val);
            return val;
        }
    }

    /**
     * 格式化成时间，HH小时mm分钟
     *
     * @param number
     * @return
     */
    public static String asDurationTime(Number number) {
        if(number.longValue() <= 0){
            return "0秒";
        }
        StringBuilder sb = new StringBuilder();
        long time = number.longValue() / 1000;
        if(time <= 0){
            return "0秒";
        }

        long h = time / 60 / 60;
        long m = time / 60 % 60;
        long s = time % 60;
        if (h != 0)
            sb.append(h + "小时");
        if (m != 0)
            sb.append(m + "分钟");
        if (s != 0)
            sb.append(s + "秒");
        return sb.toString();
    }
}
